package com.gary.shiro.aspect;

import com.alibaba.fastjson.JSON;
import com.gary.shiro.annotation.ParamLog;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

/**
 * @author peng_rui
 * @date 2022/6/27
 */
@Slf4j
@Aspect
@Component
public class ParamLogAspect {

    /**
     * 设置切入点
     */
    @Pointcut("within(@org.springframework.web.bind.annotation.RestController *)")
    public void restController() {}

    @Around("restController()")
    public void round(ProceedingJoinPoint point) throws Throwable {
        MethodSignature signature = (MethodSignature) point.getSignature();
        ParamLog paramLog = signature.getMethod().getAnnotation(ParamLog.class);
        if (paramLog == null) {
            paramLog = DefaultParamLog.class.getAnnotation(ParamLog.class);
        }
        System.out.println("paramLog.isLogReq" + paramLog.isLogReq());
        System.out.println("paramLog.isLogResp" + paramLog.isLogResp());
        System.out.println("signature : " + signature);
        Object[] args = point.getArgs();
        System.out.println(JSON.toJSONString(args));
    }

    @ParamLog
    final class DefaultParamLog {
    }
}
